<?php
namespace app\controller;
use app\BaseController;

//名人堂	Hall_of Fame
//id	id	int	自增主键
//用户id	uid	int
//描述	describe	text
//创建时间	creation_time	date_time
//创建人	creation_operator	int
//状态	stat	int	0 删除 1名人 2冥人



class FameHall extends BaseController{
    private function checkAuth($isDevelopers = false,$uid1 = null): bool
    {
        if (!empty($uid1)) $uid = $uid1;
        else $uid = $this->getUid();
        if (empty($uid)) return false;
        try {
            if(!empty($isDevelopers))$check = $this->M('user_role')->where('uid', '=', $uid)->where('role_id', '=', 2)->find();
            else $check = $this->M('user_role')->where('uid', '=', $uid)->where('role_id', '=', 1)->find();
            if (empty($check)) return false; else return true;
        } catch (DataNotFoundException|ModelNotFoundException|DbException $e) {
            return false;
        }
    }

    private function writeLog($method,$state): void
    {
        //记录调用日志
        $this->M('log_admin')->insert(['uid' => $this->getUid(), 'ip' => getUserIp(), 'time' => time(), 'method' => $method, 'state' => $state]);
    }

    //查询名人堂列表
    public function getFameHallList(){
        //stat传入1为名人堂，传入2为冥人堂，传入0为删除，在查询功能中，不得传入0，否则报错
        $stat = $this->$this->request()->get('stat');
        if (empty($stat)) $stat = 1;
        if ($stat==0) return pr(300,'查询时状态不得为0');
        //分页查询
        $page = $this->$this->request()->get('page');
        if (empty($page)) $page = 1;
        $limit = $this->$this->request()->get('limit');
        if (empty($limit)) $limit = 20;
        $list = $this->M('FameHall')->page($page,$limit)->where('stat',$stat)->select();
        return pr(200,'查询成功',$list);
        //查询【不】上传到操作日志
    }

    //添加名人堂
    public function addFameHall(){
        $uid = $this->getUid();
        if (empty($uid)) return pr(300,'请先登录');
        //判断访问者是否有权限添加名人堂
        if (!$this->checkAuth()) return pr(300, 'API_NO.AUTH') ;

        $auid = $this->$this->request()->post('auid');
        if (empty($auid)) return pr(300,'请传入被添加人的uid（目标用户id简称auid）');
        //检测目标用户是否在册

        $stat = $this->$this->request()->post('stat');
        if (empty($stat)) return pr(300,'必须设置状态');
        if (($stat!=1)and($stat!=2)) return pr(300,'添加名人堂时，状态只能为1或者2');

        $des = $this->$this->request()->post('des');
        if (empty($des)) return pr(300,'必须有描述');

        //不能把管理加入冥人堂
        if(($stat==2)and($this->checkAuth(false,$auid))){
            $log = '不能把管理'.$auid.'加入冥人堂';
            $this->writeLog('addFameHall',$log);
            return pr(300,$log);
        }

        if ($this->M('FameHall')->where('uid',$auid)->select()==null){
            //如果目标用户不在册，添加
            try{
                $this->M('FameHall')->insert([
                    'uid'=>$auid,
                    'des'=>$des,
                    'stat'=>$stat,
                    'operator'=>$uid,
                    'creation_time'=>time()
                ]);
            } catch (\Exception $e){
                return pr(300,$e->getMessage());
            }

            if ($stat == 1)
                return pr(200,'添加成功,'.$auid.'加入了名人堂');
            if ($stat == 2)
                return pr(200,'添加成功,'.$auid.'加入了冥人堂');
        }else{
            //否则更新用户信息
            try{
                $this->M('FameHall')->update([
                    'uid'=>$auid,
                    'des'=>$des,
                    'stat'=>$stat,
                    'operator'=>$uid,
                    'creation_time'=>time()]
                );

            } catch (\Exception $e){
                return pr(300,$e->getMessage());
            }
            //生成让人看的log信息
            if ($stat == 1) $log = '更新成功,'.$auid.'加入了名人堂';
            if ($stat == 2) $log = '更新成功,'.$auid.'加入了冥人堂';
            //记录操作日志
            $this->writeLog('addFameHall', $log);
            //报喜
            return pr(200,$log);
        }
    }

    public function deleteFameHall(){
        $uid = $this->getUid();
        if (empty($uid)) return pr(300,'请先登录');
        //判断访问者是否有权限删除名人堂
        if ( !$this->checkAuth()) return pr(300, 'API_NO.AUTH') ;

        $auid = $this->$this->request()->post('auid');
        if (empty($auid)) return pr(300,'请传入被添加人的uid（目标用户id简称auid）');
        //检测目标用户是否在册
        $this->M('FameHall')->where('uid',$auid)->delete();
        //生成让人看的log信息
        $log = '删除成功,'.$auid.'被移出了名人堂';
        //记录操作日志
        $this->writeLog('deleteFameHall', $log);
        //报喜
        return pr(200,$log);
    }


}